<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input v-model="searchForm.keyword" clearable placeholder="搜索" @keyup.enter.native="search" />
      </el-form-item>

      <el-form-item v-if="listQuery.type==='all'" label="订单状态">
        <selected-status
          v-model="searchForm.order_status"
          :options="orderStatus"
          @change="search"
        />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="searchForm.created_time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00','23:59:59']"
          :picker-options="{
            shortcuts: [
              {
                text: '最近一周',
                onClick: (picker) => {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', [start, end]);
                }
              },
              {
                text: '最近一个月',
                onClick: (picker) => {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                  picker.$emit('pick', [start, end]);
                }
              }]
          }"
        />
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="searchForm.pay_time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00','23:59:59']"
          :picker-options="{
            shortcuts: [
              {
                text: '最近一周',
                onClick: (picker) => {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', [start, end]);
                }
              },
              {
                text: '最近一个月',
                onClick: (picker) => {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                  picker.$emit('pick', [start, end]);
                }
              }]
          }"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="订单ID" width="95">
        <template v-slot:default="{scope,row}">
          {{ row.order_id }}
        </template>
      </el-table-column>
      <el-table-column label="用户" width="100" align="center">
        <template v-slot:default="{scope,row}">
          {{ row.username }}
        </template>
      </el-table-column>
      <el-table-column label="订单编号" width="180" align="left">
        <template v-slot:default="{scope,row}">
          {{ row.order_sn }}
        </template>
      </el-table-column>
      <el-table-column label="商品" width="180" align="left">
        <template v-slot:default="{scope,row}">

        <el-image :src="row.goods_images[0]" :preview-src-list="row.goods_images" />
        <br>
          {{ row.goods_name }}
        </template>
      </el-table-column>
      <el-table-column label="订单状态" width="130" align="center">
        <template v-slot:default="{scope,row}">
          <el-tag hit style="color: #ffffff" :color="row.order_status | statusFilter">
            {{ row.order_status_desc }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="合计原价" width="120" align="center">
        <template v-slot:default="{scope,row}">
          {{ row.total_price }}
        </template>
      </el-table-column>
      <el-table-column label="应付金额" width="120" align="center">
        <template v-slot:default="{scope,row}">
          {{ row.order_pay_price }}
        </template>
      </el-table-column>
      <el-table-column label="实付金额" width="120" align="center">
        <template v-slot:default="{scope,row}">
          {{ row.pay_price }}
        </template>
      </el-table-column>
      <el-table-column label="优惠价" width="150" align="center">
        <template v-slot:default="{scope,row}">
          {{ row.coupon_price }}
        </template>
      </el-table-column>
      <el-table-column label="支付状态" width="120" align="center">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.pay_status==1" type="success">已支付</el-tag>
          <el-tag v-if="row.pay_status==0" type="danger">未支付</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="下单时间" width="160" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.created_at || '/' }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="created_at" label="操作" fixed="right" width="150">
        <template v-slot:default="{scope,row}">
          <el-row class="row-action-btns">

            <el-button
              size="mini"
              type="primary"
              circle
              icon="svg-icon svg-icon-check"
              @click="$router.push({path:'/order/detail',query:{order_id:row.order_id}})"
            ><svg-icon color="white" :icon-class="type!=='need_check'?'detail' :'to-check'" />
            </el-button>
            <el-button
              v-if="row.order_status===0 || row.order_status === 6"
              size="mini"
              type="info"
              icon="el-icon-delete"
              circle
              @click="del(row.order_id)"
            />
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData"
    />
  </div>

</template>
<script>
import api from '@/api/order'
import Pagination from '@/components/Pagination'
import selectedStatus from '@/components/tools/selectedStatus.vue'
export default {
  components: {
    Pagination, selectedStatus
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '0': 'gray',
        '1': 'blue',
        '2': 'green',
        '3': '#e6a23c',
        '4': 'green',
        '5': 'red',
        '6': 'gray',
        '7': 'red'
      }
      return statusMap[status]
    }
  },
  props: {
    // 接收父组件传递的类型
    type: {
      type: String,
      default: 'all',
      validator(value) {
        return ['all', 'need_send', 'need_check'].includes(value)
      }
    }
  },
  data() {
    return {
      orderStatus: [
        { value: '0', label: '待付款' },
        { value: '1', label: '待发货' },
        { value: '2', label: '待收货' },
        { value: '3', label: '待评价' },
        { value: '4', label: '已完成' },
        { value: '5', label: '售后中' },
        { value: '6', label: '已关闭' },
        { value: '7', label: '待审核' }
      ],
      list: null,
      listLoading: true,
      total: 0,
      searchForm: {
        keyword: '',
        order_status: '',
        created_time: '',
        pay_time: '',
        order_sn: ''
      }
    }
  },
  computed: {
    // 使用计算属性生成listQuery，确保每次渲染时都能正确地应用过滤条件
    listQuery() {
      return {
        type: this.type, // 根据prop动态设置type
        page: this.$route.query.page || 1,
        limit: this.$route.query.limit || 10,
        sort: this.$route.query.sort || ''
      }
    }
  },
  created() {
    if (this.$route.query.hasOwnProperty('order_id')) {
      this.searchForm.keyword = this.$route.query.order_id
      Object.assign(this.listQuery, this.searchForm)
    }
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    del(order_id) {
      this.$confirm('此操作将订单删除吗, 是否继续?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ order_id: order_id }).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.fetchData()
        })
      }).catch(() => {
      })
    },
    search() {
      this.listQuery.page = 1
      Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    }
  }
}
</script>

<style>
.el-image {
  width: 46px;
  height: 46px;
}

.bs-red {
  color: #ff0000;
}

.bs-green {
  color: #089c08;
}
</style>
